import { reqAddress } from "../../api/address"
import { reqAddressDetail } from "../../api/addressedit"
import { reqConsign, reqDeliveryDetail, reqOrderAddressEdit,reqOrderCancel, reqOrderDetail,reqPayOrder } from "../../api/orderdetail"
import { reqPickupList } from "../../api/ordersubmit"
import { modal, toast } from "../../utils/extendApi"
import { getStorage } from "../../utils/storage"

// pages/orderdetail/orderdetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order:{},
    address:{},
    addressShow:false,
    deliveryShow:false,
    addressList:[],
    active:0,
    deliveryDetail:[]
  },

  editAddress(){
    this.setData({
      addressShow:true
    })


    const active=this.data.active
    if(active==0){
      this.getAddressList()
    }
    else{
      this.getPickupList()
    }
  },

  oncloseaddressShow(){
    this.setData({
      addressShow:false
    })
  },

  onclosedeliveryShow(){
    this.setData({
      deliveryShow:false
    })
  },

  
  async onLoad(options) {
    const orderId = options.orderId
    // console.log(options)
    const {data} = await reqOrderDetail(orderId)
    this.setData({
      order:data
    })
    this.getaddress()
  },

  back(){
    wx.navigateBack()
  },

  async getaddress(){
    const id = this.data.order.addressId
    const {data}  = await reqAddressDetail(id)
    this.setData({
      address:data
    })
  },


  //获取个人地址列表
  async getAddressList(){
    const {data} = await reqAddress()
    this.setData({
      addressList:data
    })
  },

  //获取团长地址列表
  async getPickupList(){
    const {data} = await reqPickupList()
    this.setData({
      addressList:data
    })
  },

  async changeAddress(e){
    const addressId = e.currentTarget.dataset.id
    // console.log(addressId)

    const {data} = await reqAddressDetail(addressId)
    // console.log(data)
    this.setData({
      address:data
    })
    this.oncloseaddressShow()

    this.changeorderAddress()
    toast({
      title:'修改成功',
      icon:'success'
    })
  },

  async changeorderAddress(){
    const param={
      addressId:this.data.address.addressId,
      orderId:this.data.order.orderId
    }

    const {data} = await reqOrderAddressEdit(param)
    console.log(data)

  },

  navigatetoAssess(){
    const orderId = this.data.order.orderId
    wx.navigateTo({
      url: `/pages/assess/assess?orderId=${orderId}`,
    })
  },

  async ensurecancel(){
    const res = await wx.modal({content:'确认取消订单？'})
    if (res){
      this.cancelOrder()
    }
  },

  async cancelOrder(){
    const orderId = this.data.order.orderId

    const {code} = await reqOrderCancel(orderId)
    if(code){
      toast({
        title:'取消成功',
        icon:'success'
      })
    }else{
      toast({
        title:'取消订单失败',
        icon:'error'
      })
    }

    wx.navigateBack()
    },

  async onchangeactive(e){
    const index = e.detail.index
    // console.log(index)
    this.setData({
      active:index
    })
    if(index==0){
      this.getAddressList()
    }
    else{
      this.getPickupList()
    }
  },

  async ensureconsign(){
    const res = await wx.modal({content:'确认收货？'})
    if(res){
      this.consign()
    }
  },

  async consign(){
    const orderId = this.data.order.orderId
    const {code} = await reqConsign(orderId)
    if(code){
      toast({
        title:'确认收货成功',
        icon:'success'
      })
    }
    wx.navigateBack();
  },

  /**
   * 查看物流详细信息
   */
  async checkdelivery(){
    const orderId = "ORD-0001"
    //测试代码 待仓储和配送协商后再修改为实际订单ID
    const {data} = await reqDeliveryDetail(orderId)
    data.forEach(obj =>{
      obj.text = obj.timeStamp + "【" +obj.location + "】" + obj.remarks
    })
    this.setData({
      deliveryDetail:data
    })
    this.setData({
      deliveryShow:true
    })
  },

  async gotopay(){
    const orderId = this.data.order.orderId
    await reqPayOrder(orderId)
    wx.navigateBack();
    toast({
      title:'支付成功',
      icon:'success'
    })
  }

})